package com.csap.framework.oauth2.server.service.impl;

import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.oauth2.server.entity.PermissionsEntity;
import com.csap.framework.oauth2.server.entity.ScopePermissionsEntity;
import com.csap.framework.oauth2.server.model.ScopePermissionsModel;
import com.csap.framework.oauth2.server.mybatis.ScopePermissionsMapper;
import com.csap.framework.oauth2.server.service.PermissionsService;
import com.csap.framework.oauth2.server.service.ScopePermissionsService;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 作用域权限
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-07-25 23:51:30
 */
@Service
@Description(value = "作用域权限ServiceImpl")
public class ScopePermissionsServiceImpl extends BaseCsapServiceImpl<ScopePermissionsMapper, ScopePermissionsEntity, ScopePermissionsModel, Long> implements ScopePermissionsService {
    @Resource
    private PermissionsService permissionsService;

    @Override
    public Set<String> permissions(Long scopeId) {
        return lambdaQuery()
                .select(ScopePermissionsEntity::getPermissionsId)
                .eq(ScopePermissionsEntity::getScopeId, scopeId)
                .listMany(permissionsService)
                .eq(PermissionsEntity::getPermissionsId, ScopePermissionsEntity::getPermissionsId)
                .list()
                .end(ScopePermissionsEntity::getPermissions)
                .list()
                .stream()
                .map(ScopePermissionsEntity::getPermissions)
                .map(PermissionsEntity::getValue)
                .collect(Collectors.toSet());
    }
}
